/*
 * @Author: blak-kong
 * @Date: 2019-12-03 09:51:44
 * @Email: 546598185@qq.com
 */


// models
const DevServe = require('../models/DevServe');
const { SuccessModel, ErrorModel } = require("../models/resModel")


module.exports = {
    async query_page (ctx, next) {
        let {size = 10, page = 1} = ctx.query;

        try {
            let options = {
                skip: Number((page-1)*size),
                limit: Number(size),
                sort:{"create_time":"-1"}
            }
            let res = await DevServe.find({},null,options);     // 获取数据列表
            let total = await DevServe.count();                 // 获取文档总数-官方文档count()已弃用  但mongoos有兼容
            ctx.body = new SuccessModel({
                list: res,
                pagination: {
                    total,
                    page : Number(page),
                    size : Number(size)
                }
            }, '获取分页数据成功')
            return
        } catch(e) {
            console.log(e);
            ctx.body = new SuccessModel({}, "获取失败，服务器异常，请稍后再试！")
            return
        }
    },
    async query (ctx, next) {
        let res = await DevServe.findOne({_id: ctx.params.id})
        if (res.id) {
            ctx.body = new SuccessModel(res)
            return
        }
        ctx.body = new ErrorModel({}, "服务器错误！")
    },
    
    async update (ctx, next) {
        const doc = await DevServe.findOne({_id: ctx.params.id});
        doc.name = ctx.request.body.name,
        doc.content = ctx.request.body.content,
        doc.description = ctx.request.body.description
        let res = await doc.save()
        if (res.id) {
            ctx.body = new SuccessModel({},  "更新成功！")
            return
        }
        ctx.body = new ErrorModel({}, "更新失败！请稍后重试！")
    },
    async delete (ctx, next) {
        let res = await DevServe.deleteOne({_id: ctx.params.id})
        if (res.n && res.ok) {
            ctx.body = new SuccessModel({}, "删除成功！")
            return
        }
        ctx.body = new ErrorModel({}, "删除失败！请稍后重试！")
        return
    },

    async add (ctx, next) {
        // save() 需要先给model赋值实例化
        let devServe = new DevServe(ctx.request.body)
        let res = await devServe.save()
        if (res.id) {
            ctx.body = new SuccessModel({}, "创建成功！")
            return
        }
        ctx.body = new ErrorModel({}, "创建失败！请稍后重试！")
    },

    async uccn (ctx, next) {
        let {size = 10, page = 1} = ctx.query;

        try {
            let options = {
                skip: Number((page-1)*size),
                limit: Number(size),
                sort:{"create_time":"-1"}
            }
            let res = await DevServe.find({},null,options);     // 获取数据列表
            let total = await DevServe.count();                 // 获取文档总数-官方文档count()已弃用  但mongoos有兼容
            ctx.body = new SuccessModel({
                list: res,
                pagination: {
                    total,
                    page : Number(page),
                    size : Number(size)
                }
            }, '获取页面数据成功')
            return
        } catch(e) {
            console.log(e);
            ctx.body = new SuccessModel({}, "获取失败，服务器异常，请稍后再试！")
            return
        }
    }
}